<?php

/**
 * post actions.
 *
 * @package    hacride
 * @subpackage post
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class postActions extends sfActions
{
  /**
   * Executes index action
   *
   */
  public function executeIndex()
  {
    $this->forward('post', 'listByParams');
  }

  public function executeListByParams()
  {

    if ($this->getRequestParameter('sf_culture') 
    and $this->getRequestParameter('sf_culture') == 'fr' )
    {
      $this->getUser()->setCulture('fr');
    }
    else
    {
      $this->getUser()->setCulture('en');
      
    }
    
    if (!$this->isValidPublishedAt())
    {
      $this->forward404();
    }


    $this->posts = PostPeer::getNewsByParams(
                            5,
                            $this->getDefaultLanguage(),
                            $this->getRequestParameter('year'),
                            $this->getRequestParameter('month'),
                            $this->getRequestParameter('day')
                            );
                            
    $this->archives = PostPeer::getListOfArchives();
    
  }

  public function executeDisplayOnePost()
  {

    if (!$this->isValidPublishedAt())
    {
      $this->forward404();
    }

    $this->post = PostPeer::getNewsByParams(
                            10,
                            $this->getDefaultLanguage(),
                            $this->getRequestParameter('year'),
                            $this->getRequestParameter('month'),
                            $this->getRequestParameter('day'),
                            $this->getRequestParameter('slug')
                            );
                            
    $this->new_comment = new Comment();

    $this->archives = PostPeer::getListOfArchives();
    
    if  ($this->getRequest()->getMethod() == sfRequest::POST)
  	{
  	  $comment = new Comment();
  	        	  
  	  $comment->fromArray(array(
  	    'Name' => $this->getRequestParameter('name'),
  	    'Mail' => $this->getRequestParameter('mail'),
  	    'Web' => $this->getRequestParameter('web'),
  	    'Body' => $this->getRequestParameter('body'),
  	    'UserIp' => http::realIP(),
  	    'UserAgent' => $_SERVER['HTTP_USER_AGENT']
  	    )
  	  );
  	  
  	  $comment->setPost($this->post);
  	  
  	  // on vérifie par AKISMET si le commentaire n'est pas un SPAM
  	  $akismet = new Zend_Service_Akismet(sfConfig::get('app_akismet_key'), sfConfig::get('app_url'));
  	  
  	  $akismet_data = array(
  	    'user_ip'              => http::realIP(),
        'user_agent'           => $_SERVER['HTTP_USER_AGENT'],
        'comment_type'         => 'comment',
        'comment_author'       => $this->getRequestParameter('name'),
        'comment_author_email' => $this->getRequestParameter('mail'),
        'comment_content'      => $this->getRequestParameter('body')
  	  );
  	  
  	  if ($akismet->isSpam($akismet_data))
  	  {
  	    $comment->setIsSpam(true);
  	    $comment->save();
  	    
  	    $this->setFlash('notice', 'Sorry, but we think you\'re a spammer.', false);
  	    $this->redirect($this->post->getPermalink().'#form-comment');
  	  }
  	  else 
  	  {
  	    $comment->setIsSpam(false);
  	    $comment->save();
  	  
  	    $this->setFlash('notice', 'Your comment has been added.', false);
  	    $this->redirect($this->post->getPermalink().'#comment_'.$comment->getId());
  	  }
  	  
  	}
   
  }
  
  public function handleErrorDisplayOnePost()
  {
  	$this->post = PostPeer::getNewsByParams(
                            10,
                            $this->getDefaultLanguage(),
                            $this->getRequestParameter('year'),
                            $this->getRequestParameter('month'),
                            $this->getRequestParameter('day'),
                            $this->getRequestParameter('slug')
                            );
    $this->new_comment = new Comment();
    
    $this->archives = PostPeer::getListOfArchives();
    
    return sfView::SUCCESS;
  }
  
  public function executeListByTag()
  {
    if (!$this->getRequestParameter('tag'))
    {
      $this->forward404();
    }
    
    $this->posts = TagPeer::getTaggedWith(array($this->getRequestParameter('tag')), array('model' => 'Post'));
    $this->forward404Unless($this->posts);
    
    // preload des tags pour éviter la redondance des requetes
    sfPropelActAsTaggableBehavior::preloadTags($this->posts);
    
    $this->tag = $this->getRequestParameter('tag');
    
    $this->archives = PostPeer::getListOfArchives();
    
    $this->setTemplate('listByParams');
  }

  /**
   * teste si la date de publication est inférieure ou égale à la
   * date d'aujourdhui
   *
   * @return boolean true si <=
   */
  private function isValidPublishedAt()
  {
    $year = $this->getRequestParameter('year');
    $month = $this->getRequestParameter('month');
    $day = $this->getRequestParameter('day');

    if ($year)
    {
      if ($month)
      {
        if ($day)
        {
          return ($year.'-'.$month.'-'.$day > date('Y-m-d')) ? false : true;
        }
        else
        {
          return ($year.'-'.$month > date('Y-m')) ? false : true;
        }
      }
      else
      {
        return ($year > date('Y')) ? false : true;
      }
    }
    else
    {
      return true;
    }
  }

  /**
   * retourne la date par défaut
   */
  private function getDefaultLanguage()
  {
    return $this->getUser()->getCulture() == 'fr' ? 'fr' : 'en';
  }

}
